/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AccountStatus;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.Names;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapProvAttrCallback
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  49 */     provUtil = new LdapProvTestUtil();
/*  50 */     prov = provUtil.getProv();
/*  51 */     domain = provUtil.createDomain(baseDomainName(), null);
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  56 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart) throws Exception {
/*  60 */     return createAccount(localPart, null);
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart, Map<String, Object> attrs) throws Exception
/*     */   {
/*  65 */     return provUtil.createAccount(localPart, domain, attrs);
/*     */   }
/*     */   
/*     */   private void deleteAccount(Account acct) throws Exception {
/*  69 */     provUtil.deleteAccount(acct);
/*     */   }
/*     */   
/*     */   private DistributionList createDistributionList(String localpart) throws Exception {
/*  73 */     return createDistributionList(localpart, null);
/*     */   }
/*     */   
/*     */   private DistributionList createDistributionList(String localPart, Map<String, Object> attrs) throws Exception
/*     */   {
/*  78 */     return provUtil.createDistributionList(localPart, domain, attrs);
/*     */   }
/*     */   
/*     */   private DynamicGroup createDynamicGroup(String localPart, Map<String, Object> attrs) throws Exception
/*     */   {
/*  83 */     return provUtil.createDynamicGroup(localPart, domain, attrs);
/*     */   }
/*     */   
/*     */   private DynamicGroup createDynamicGroup(String localPart) throws Exception
/*     */   {
/*  88 */     return createDynamicGroup(localPart, null);
/*     */   }
/*     */   
/*     */   private void deleteDynamicGroup(DynamicGroup group) throws Exception {
/*  92 */     provUtil.deleteDynamicGroup(group);
/*     */   }
/*     */   
/*     */   private DynamicGroup refresh(DynamicGroup group) throws Exception {
/*  96 */     prov.flushCache(CacheEntryType.group, null);
/*  97 */     group = (DynamicGroup)prov.getGroup(Key.DistributionListBy.id, group.getId());
/*  98 */     Assert.assertNotNull(group);
/*  99 */     return group;
/*     */   }
/*     */   
/*     */   private Domain createDomain(String subDomainSegment, Map<String, Object> attrs) throws Exception
/*     */   {
/* 104 */     return provUtil.createDomain(subDomainSegment + "." + baseDomainName(), attrs);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void accountStatus() throws Exception {
/* 109 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart("account"));
/* 110 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/*     */     
/* 112 */     String ALIAS_LOCALPART_1 = Names.makeAliasNameLocalPart(genAcctNameLocalPart("alias-1"));
/* 113 */     String ALIAS_NAME_1 = TestUtil.getAddress(ALIAS_LOCALPART_1, domain.getName()).toLowerCase();
/* 114 */     String ALIAS_LOCALPART_2 = Names.makeAliasNameLocalPart(genAcctNameLocalPart("alias-2"));
/* 115 */     String ALIAS_NAME_2 = TestUtil.getAddress(ALIAS_LOCALPART_2, domain.getName()).toLowerCase();
/*     */     
/* 117 */     prov.addAlias(acct, ALIAS_NAME_1);
/* 118 */     prov.addAlias(acct, ALIAS_NAME_2);
/*     */     
/* 120 */     String DL_NAME_LOCALPART = Names.makeDLNameLocalPart("accountStatus-dl");
/* 121 */     DistributionList dl = createDistributionList(DL_NAME_LOCALPART);
/*     */     
/* 123 */     dl.addMembers(new String[] { acct.getName(), ALIAS_NAME_1, ALIAS_NAME_2 });
/*     */     
/* 125 */     prov.flushCache(CacheEntryType.account, null);
/* 126 */     prov.flushCache(CacheEntryType.group, null);
/*     */     
/* 128 */     Set<String> allMembers = dl.getAllMembersSet();
/* 129 */     Assert.assertEquals(3L, allMembers.size());
/* 130 */     Assert.assertTrue(allMembers.contains(acct.getName()));
/* 131 */     Assert.assertTrue(allMembers.contains(ALIAS_NAME_1));
/* 132 */     Assert.assertTrue(allMembers.contains(ALIAS_NAME_2));
/*     */     
/* 134 */     Map<String, Object> attrs = new HashMap();
/* 135 */     attrs.put("zimbraAccountStatus", ZAttrProvisioning.AccountStatus.closed.name());
/* 136 */     prov.modifyAttrs(acct, attrs);
/*     */     
/* 138 */     prov.flushCache(CacheEntryType.account, null);
/* 139 */     prov.flushCache(CacheEntryType.group, null);
/*     */     
/* 141 */     dl = prov.get(Key.DistributionListBy.id, dl.getId());
/*     */     
/* 143 */     allMembers = dl.getAllMembersSet();
/* 144 */     Assert.assertEquals(0L, allMembers.size());
/*     */     
/* 146 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   private void verifyIsNotACLGroup(DynamicGroup group, String expectedMemberURL) {
/* 150 */     Assert.assertEquals("FALSE", group.getAttr("zimbraIsACLGroup"));
/* 151 */     Assert.assertEquals(expectedMemberURL, group.getAttr("memberURL"));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void zimbraIsACLGroupAndMemberURLCreate() throws Exception {
/* 156 */     String SOME_URL = "blah";
/*     */     
/* 158 */     Map<String, Object> attrs = Maps.newHashMap();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 163 */     attrs.clear();
/* 164 */     attrs.put("zimbraIsACLGroup", "FALSE");
/* 165 */     attrs.put("memberURL", SOME_URL);
/* 166 */     DynamicGroup group = createDynamicGroup(genGroupNameLocalPart("1"), attrs);
/* 167 */     verifyIsNotACLGroup(group, SOME_URL);
/* 168 */     deleteDynamicGroup(group);
/*     */     
/*     */ 
/* 171 */     boolean caughtException = false;
/* 172 */     attrs.clear();
/* 173 */     attrs.put("zimbraIsACLGroup", "TRUE");
/* 174 */     attrs.put("memberURL", SOME_URL);
/*     */     try {
/* 176 */       group = createDynamicGroup(genGroupNameLocalPart("2"), attrs);
/*     */     } catch (ServiceException e) {
/* 178 */       if ("service.INVALID_REQUEST".equals(e.getCode())) {
/* 179 */         caughtException = true;
/*     */       } else {
/* 181 */         throw e;
/*     */       }
/*     */     }
/* 184 */     Assert.assertTrue(caughtException);
/*     */     
/*     */ 
/*     */ 
/* 188 */     caughtException = false;
/* 189 */     attrs.clear();
/* 190 */     attrs.put("memberURL", SOME_URL);
/*     */     try {
/* 192 */       group = createDynamicGroup(genGroupNameLocalPart("3"), attrs);
/*     */     } catch (ServiceException e) {
/* 194 */       if ("service.INVALID_REQUEST".equals(e.getCode())) {
/* 195 */         caughtException = true;
/*     */       } else {
/* 197 */         throw e;
/*     */       }
/*     */     }
/* 200 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void zimbraIsACLGroupAndMemberURLModify() throws Exception {
/* 205 */     String SOME_URL = "blah";
/* 206 */     String SOME_URL_2 = "blah blah";
/*     */     
/* 208 */     Map<String, Object> attrs = Maps.newHashMap();
/*     */     
/*     */ 
/*     */ 
/* 212 */     DynamicGroup group = createDynamicGroup(genGroupNameLocalPart("3"));
/* 213 */     attrs.clear();
/* 214 */     attrs.put("memberURL", SOME_URL);
/* 215 */     boolean caughtException = false;
/*     */     try {
/* 217 */       prov.modifyAttrs(group, attrs, true);
/*     */     } catch (ServiceException e) {
/* 219 */       if ("service.INVALID_REQUEST".equals(e.getCode())) {
/* 220 */         caughtException = true;
/*     */       } else {
/* 222 */         throw e;
/*     */       }
/*     */     }
/* 225 */     Assert.assertTrue(caughtException);
/* 226 */     deleteDynamicGroup(group);
/*     */     
/* 228 */     group = createDynamicGroup(genGroupNameLocalPart("4"));
/* 229 */     attrs.clear();
/* 230 */     attrs.put("zimbraIsACLGroup", "TRUE");
/* 231 */     attrs.put("memberURL", SOME_URL);
/* 232 */     caughtException = false;
/*     */     try {
/* 234 */       prov.modifyAttrs(group, attrs);
/*     */     } catch (ServiceException e) {
/* 236 */       if ("service.INVALID_REQUEST".equals(e.getCode())) {
/* 237 */         caughtException = true;
/*     */       } else {
/* 239 */         throw e;
/*     */       }
/*     */     }
/* 242 */     Assert.assertTrue(caughtException);
/* 243 */     deleteDynamicGroup(group);
/*     */     
/* 245 */     attrs.clear();
/* 246 */     attrs.put("zimbraIsACLGroup", "FALSE");
/* 247 */     attrs.put("memberURL", SOME_URL);
/* 248 */     group = createDynamicGroup(genGroupNameLocalPart("8"), attrs);
/* 249 */     attrs.clear();
/* 250 */     attrs.put("memberURL", SOME_URL_2);
/* 251 */     prov.modifyAttrs(group, attrs, true);
/* 252 */     verifyIsNotACLGroup(group, SOME_URL_2);
/* 253 */     deleteDynamicGroup(group);
/*     */     
/* 255 */     attrs.clear();
/* 256 */     attrs.put("zimbraIsACLGroup", "FALSE");
/* 257 */     attrs.put("memberURL", SOME_URL);
/* 258 */     group = createDynamicGroup(genGroupNameLocalPart("9"), attrs);
/* 259 */     attrs.clear();
/* 260 */     attrs.put("zimbraIsACLGroup", "TRUE");
/* 261 */     attrs.put("memberURL", SOME_URL);
/* 262 */     caughtException = false;
/*     */     try {
/* 264 */       prov.modifyAttrs(group, attrs, true);
/*     */     } catch (ServiceException e) {
/* 266 */       if ("service.INVALID_REQUEST".equals(e.getCode())) {
/* 267 */         caughtException = true;
/*     */       } else {
/* 269 */         throw e;
/*     */       }
/*     */     }
/* 272 */     Assert.assertTrue(caughtException);
/* 273 */     deleteDynamicGroup(group);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void authMech() throws Exception
/*     */   {
/* 279 */     Map<String, Object> attrs = Maps.newHashMap();
/* 280 */     attrs.put("zimbraAuthMech", "custom");
/* 281 */     Domain testDomain = createDomain("authMech", attrs);
/*     */     
/*     */ 
/* 284 */     attrs.clear();
/* 285 */     attrs.put("zimbraAuthMech", "bad");
/* 286 */     boolean caughtException = false;
/*     */     try {
/* 288 */       prov.modifyAttrs(testDomain, attrs);
/*     */     } catch (ServiceException e) {
/* 290 */       if ("service.INVALID_REQUEST".equals(e.getCode())) {
/* 291 */         caughtException = true;
/*     */       }
/*     */     }
/* 294 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvAttrCallback.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */